package com.btpn.custody.web.m007.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.bean.ParameterBeans;
import com.btpn.custody.common.Constant;
import com.btpn.custody.dao.IUser;
import com.btpn.custody.entity.User;
import com.btpn.custody.service.IBranchService;
import com.btpn.custody.service.IMasterService;
import com.btpn.custody.web.m002.model.BranchModel;
import com.btpn.custody.web.m002.model.Param;
import com.btpn.infra.utils.MyDateUtils;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "custody")
@Results({
	@Result(name="input", type="redirectAction", params = {"actionName", "receiving-docs"} )
})
public class ReceivingDocsAction extends ActionSupport {
	
	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = 5346716776484276333L;

	/*=============================================== M E M B E R =============================================*/		
	@Autowired
	private IBranchService branchService;

	@Autowired
	private IMasterService masterService;
	
	@Autowired
	private IUser userDaoHibernate;
	
	@Autowired
	private ParameterBeans parameterBeans;
		
	private String areaName;
	private BranchModel model;
	private String _chk[];
	
	// SEARCH
	private String keyword, fromDate, toDate, accCode, branchCode;	
	
	// parameter
	private Map<String, String> accTypeMap;			// P0003
	private List<Param> branchList;			// list of KCP/KC
	
	/*=============================================== B E H A V I O R =========================================*/
	/*
	 * PURPOSE : show MD where STATUS_MD 'NPY' and by branchId
	 */
	@Action(value = "/receiving-docs", results = { 
			@Result(name="success", location="t_receiving_docs") })
	public String showReceivingDocs() throws Exception {
		
		User user = userDaoHibernate.get(SecurityContextHolder.getContext().getAuthentication().getName());
		branchList = branchService.findBranchMap(user.getArea().getId(), Constant.STATUS__MD_NPY);
		accTypeMap = parameterBeans.getmACC_TYPE(); 
		keyword = null; fromDate = null; toDate = null; accCode = null; branchCode = null;
		
		return SUCCESS;
	}	
	
	@Action(value = "/searchReceiving", results = { 
			@Result(name="success", location="t_receiving_docs")
	})
	public String searchReceiving() throws Exception {
		Date from = MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, fromDate);
		Date to = MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, toDate);
		System.out.println("fromDate: "+fromDate);
		System.out.println("toDate: "+toDate);
		System.out.println("from: "+from);
		System.out.println("to: "+to);
		User user = userDaoHibernate.get(SecurityContextHolder.getContext().getAuthentication().getName());
		model = branchService.search(user.getArea().getId(), Constant.STATUS__MD_NPY, keyword.trim(), branchCode, accCode, "-1", from, to);
		
		branchList = branchService.findBranchMap(user.getArea().getId(), Constant.STATUS__MD_NPY);
		accTypeMap = parameterBeans.getmACC_TYPE(); 
		
		return SUCCESS;
	}
	
	/*
	 * update status from 'NPY' to 'Received'
	 */
	@Action(value = "/setReceived", results = { 
		@Result(name="success", type="redirectAction", params = {"actionName", "receiving-docs"} )
	})
	public String setReceived() throws Exception {
		
		masterService.updateStatuses(_chk, Constant.STATUS__MD_RECEIVED);
		addActionMessage("Success");
		
		return SUCCESS;
	}
	
	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public BranchModel getModel() {
		return model;
	}

	public void setModel(BranchModel model) {
		this.model = model;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public String[] get_chk() {
		return _chk;
	}

	public void set_chk(String[] _chk) {
		this._chk = _chk;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getAccCode() {
		return accCode;
	}

	public void setAccCode(String accCode) {
		this.accCode = accCode;
	}

	public String getBranchCode() {
		return branchCode;
	}

	public void setBranchCode(String branchCode) {
		this.branchCode = branchCode;
	}

	public Map<String, String> getAccTypeMap() {
		return accTypeMap;
	}

	public void setAccTypeMap(Map<String, String> accTypeMap) {
		this.accTypeMap = accTypeMap;
	}

	public List<Param> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<Param> branchList) {
		this.branchList = branchList;
	}
}
